package com.tedu.weibo.controller;

import com.tedu.weibo.comment.result.JsonResult;
import com.tedu.weibo.comment.result.StatusCode;
import com.tedu.weibo.mapper.UserMapper;
import com.tedu.weibo.pojo.dto.UserLoginDTO;
import com.tedu.weibo.pojo.dto.UserRegDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("用户注册")
    @PostMapping("/reg")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){

        String username = userRegDTO.getUsername();
        User findUser=userMapper.selectByUsername(username);
        if (findUser!=null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);

        //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.sucess();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){  //@RequestBody  告诉数据是以Json格式传输
        /*
        if(userLoginDTO.getUsername()==null||userLoginDTO.getUsername().isEmpty()){
//            throw new RuntimeException("用户名不能为空");
            throw new IllegalArgumentException("用户名不能为空");
        }

         */




        User user=userMapper.selectByUsername(userLoginDTO.getUsername());
        if (user!=null){
            if (user.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",user);
                //return new JsonResult(StatusCode.OPERATION_SUCCESS);
                return JsonResult.sucess();
            }else {
                return new JsonResult(StatusCode.PASSWORD_ERROR);
            }
        }else {
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }
    }

    @ApiOperation("当前用户显示")
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        User user=(User)session.getAttribute("user");
        if (user==null){
            //return  new JsonResult(StatusCode.OPERATION_SUCCESS);
            return JsonResult.sucess();
        }else {
            UserVO userVO=new UserVO();
            BeanUtils.copyProperties(user,userVO);

            //return new JsonResult(StatusCode.OPERATION_SUCCESS,userVO);
            return JsonResult.sucess(userVO);
        }


    }


    @ApiOperation("退出登录")
    @GetMapping("/logout")
public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
}





}
